import telebot
import openai
import requests
import os

#
#
#
# # Bot Token
# TOKEN = '7702930752:AAGYV0-kGJgMTavSPKxqdgo8tkR_gdthYJI'
# bot = telebot.TeleBot(TOKEN)
#
# # 设置代理（如不需要可注释）
# telebot.apihelper.proxy = {
#     'http': 'http://127.0.0.1:7897',
#     'https': 'http://127.0.0.1:7897',
# }
# os.environ["HTTPS_PROXY"] = "http://127.0.0.1:7897"
# # LLM 调用函数
# def ask_llm(prompt):
#     try:
#         url = "https://hydsoft-gpt-4o.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-02-01"
#         headers = {
#             "Content-Type": "application/json",
#             "api-key": "AWYrdx7jGx84uykKjJL1BYCaLP9UPUKWMM3Y9w7kS17PyoDlTF12JQQJ99BAAC4f1cMXJ3w3AAABACOGVbCz"
#         }
#         data = {
#             "messages": [{"role": "user", "content": prompt}],
#             "temperature": 0.7,
#             "max_tokens": 1000
#         }
#
#         response = requests.post(url, headers=headers, json=data, timeout=30)
#         print(response.json()['choices'][0]["message"]["content"])
#         return response.json()['choices'][0]["message"]["content"]
#     except Exception as e:
#         print("LLM Error:", e)
#         return "抱歉，AI 处理时出现错误。"
#
# # /start 命令处理器
# @bot.message_handler(commands=['start'])
# def start_message(message):
#     bot.send_message(message.chat.id, "你好，欢迎使用这个 AI Bot！")
#
# # 处理文本消息 + LLM
# @bot.message_handler(content_types=['text'])
# def handle_text(message):
#     user = message.from_user.first_name
#     text = message.text
#     chat_id = message.chat.id
#     print(f"[文本] 来自 {user} ({chat_id}): {text}")
#
#     # 调用 LLM 处理
#     llm_response = ask_llm(text)
#     bot.reply_to(message, llm_response)
#
# # 其他类型略（你已有处理）…


API_TOKEN = '7702930752:AAGYV0-kGJgMTavSPKxqdgo8tkR_gdthYJI'
bot = telebot.TeleBot(API_TOKEN)

# 获取机器人的用户名（比如 bot 的 username 是 mytestbot）
bot_username = 'zynth_core_bot'  # 不要带 @，直接写用户名
#
#
# @bot.message_handler(func=lambda message: True)
# def handle_message(message):
#     if message.chat.type in ["group", "supergroup"]:
#         # 群组中，判断是否 @ 了机器人
#
#         if f"@{bot_username}" in message.text:
#             bot.reply_to(message, "你 @ 了我，我来了～ 🤖")
#     else:
#         # 私聊中，直接回复
#         bot.reply_to(message, "你好，我是机器人！")
#
#
# # 启动 Bot
# print("🤖 Bot 正在运行...")
# bot.polling(none_stop=True)


# import telebot
#
# # 替换成你的 Bot Token
# BOT_TOKEN = "7702930752:AAGYV0-kGJgMTavSPKxqdgo8tkR_gdthYJI"
# bot = telebot.TeleBot(BOT_TOKEN)
#
# # 群聊 ID（比如 -1001234567890）
# # group_chat_id = -4951189565
# group_chat_id = -4805059187
#
# # 主动发送消息
# bot.send_message(group_chat_id, "大家好，我是机器人 🤖")
#
# print("消息已发送！")


# import telebot
#
# # 替换成你的 Bot Token
# BOT_TOKEN = "7702930752:AAGYV0-kGJgMTavSPKxqdgo8tkR_gdthYJI"
# bot = telebot.TeleBot(BOT_TOKEN)
#
# # 群聊 ID（必须是机器人已加入的群）
# group_chat_id = -4805059187
#
# # PDF 文件路径
# pdf_path = r"D:\yuanbei\data\解密文件\4.pdf"
#
# # 发送 PDF
# with open(pdf_path, 'rb') as pdf_file:
#     bot.send_document(group_chat_id, pdf_file, caption='''d1082508070309454090126
# 13651409
# Withdrawal Completed
# YD8-Deepay
# 2025-08-07 03:09:45''')
#
# print("PDF 已发送！")

#
# import telebot
#
# # 替换成你的 Bot Token
# BOT_TOKEN = "7702930752:AAGYV0-kGJgMTavSPKxqdgo8tkR_gdthYJI"
# bot = telebot.TeleBot(BOT_TOKEN)
#
# # 群聊 ID（必须是机器人已加入的群）
# group_chat_id = -4805059187
#
# # 图片文件路径
# image_path = r"D:\yuanbei\客服问答机器人 需求表\imgs\1.jpg"
#
# # 发送图片
# with open(image_path, 'rb') as image_file:
#     bot.send_photo(group_chat_id, image_file, caption='''你好请查收''')
#
# print("图片已发送！")


import time
import threading
import telebot

bot = telebot.TeleBot("7702930752:AAGYV0-kGJgMTavSPKxqdgo8tkR_gdthYJI")

media_cache = {}  # {group_id: {"photos": [], "videos": []}}

def process_group(group_id):
    """定时处理同一 media_group 的消息"""
    print("group_id", group_id)
    group = media_cache.pop(group_id, None)
    if not group:
        return
    print("group",group)

    photos = group.get("photos", [])
    videos = group.get("videos", [])
    print("photos",photos[0])
    print("videos",videos[0])

    # --- 三种情况 ---
    if photos and videos:
        print("📸+🎥 情况1: 图片 + 视频")
    elif photos:
        print("📸 情况2: 只有图片")
    elif videos:
        print("🎥 情况3: 只有视频")


@bot.message_handler(content_types=['photo', 'video'])
def handle_media(message):
    # 如果没有 group_id，说明是单条消息（单张图片或单个视频）
    group_id = message.media_group_id or message.message_id

    if group_id not in media_cache:
        media_cache[group_id] = {"photos": [], "videos": []}
        # 启动定时器，延迟1s 再处理，确保同一个相册的所有消息都收齐
        threading.Timer(1.0, process_group, args=(group_id,)).start()

    # 收集消息
    if message.content_type == "photo":
        media_cache[group_id]["photos"].append(message)
    elif message.content_type == "video":
        media_cache[group_id]["videos"].append(message)


# 启动 Bot
print("🤖 Bot 正在运行...")
bot.polling(none_stop=True)
